package advanced.class_append;

/**
 * 左神进阶班第五课
 *
 * 题目描述：
 *      给定一颗树，请你返回此树种的最大值和最小值
 */
public class Code_05_MinMaxTreeNode {

    static class TreeNode {
        int value;
        TreeNode left;
        TreeNode right;

        public TreeNode(int value) {
            this.value = value;
        }
    }

    static class ResultData{
        int min;
        int max;

        public ResultData(int min, int max) {
            this.min = min;
            this.max = max;
        }
    }
    public static int[] getMinMaxNode(TreeNode root){
        if(root == null) return null;
        ResultData res = getAns(root);
        return new int[]{res.min,res.max};
    }

    private static ResultData getAns(TreeNode root){
        if(root == null) return new ResultData(Integer.MAX_VALUE,Integer.MIN_VALUE);
        ResultData l = getAns(root.left);
        ResultData r = getAns(root.right);
        return new ResultData(Math.min(Math.min(l.min,r.min),root.value),
                              Math.max(Math.max(l.max,r.max),root.value));
    }
}
